package com.coderman.databole.service;

import com.coderman.daoclient.dao.impl.IDaoImpl;
import com.coderman.databole.model.entity.BoleUserEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by 58-daojia on 2017-12-27.
 *
 * @author: fanchunshuai
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class ReadUserToDBService {
    private  IDaoImpl<BoleUserEntity,Long> userEntityIDao = new IDaoImpl<>();

    public void readUserFromHtml2DB(String filePath) throws Exception {
        File file = new File(filePath);
        BoleUserEntity userEntity = new BoleUserEntity();
        Document document = null;
        String fileName = file.getName().replace(".html","");

        document = Jsoup.parse(file, "UTF-8");

        String authorTag = userEntity.getXpathMap().get("nickName").replace("$",fileName);

        String  authorName = Xsoup.compile(authorTag).evaluate(document).get();

        if(authorName.contains("本文作者")){

            String boleBlogIndexTag = userEntity.getXpathMap().get("boleBlogIndex").replace("$",fileName);
            //作者博客主页列表地址
            String  boleBlogIndex = Xsoup.compile(boleBlogIndexTag).evaluate(document).get();
            userEntity.setBoleBlogIndex(boleBlogIndex);
            userEntity.setBoleBlogIndex(boleBlogIndex);
            String [] array = boleBlogIndex.split("/");

            userEntity.setUserName(array[array.length - 1]);
            userEntity.setBoleIndex("http://www.jobbole.com/members/"+array[array.length - 1]+"/");

            List<BoleUserEntity> list = userEntityIDao.getList(BoleUserEntity.class,"select * from bole_user where user_name = '"+userEntity.getUserName()+"'");
            if(list == null || list.isEmpty()){
                userEntityIDao.insertEntity(userEntity);
            }


        }else if(authorName.contains("翻译自")){


            String boleBlogIndexTag = userEntity.getXpathMap().get("boleBlogIndex").replace("$",fileName);
            //作者博客主页列表地址
            String  boleBlogIndex = Xsoup.compile(boleBlogIndexTag).evaluate(document).get();
            userEntity.setBoleBlogIndex(boleBlogIndex);
            String [] array = boleBlogIndex.split("/");

            userEntity.setUserName(array[array.length - 1]);
            userEntity.setBoleIndex("http://www.jobbole.com/members/"+array[array.length - 1]+"/");
            List<BoleUserEntity> list = userEntityIDao.getList(BoleUserEntity.class,"select * from bole_user where user_name = '"+userEntity.getUserName()+"'");
            if(list == null || list.isEmpty()){
                userEntityIDao.insertEntity(userEntity);
            }

        }

    }

    public static void main(String[] args) {

        ReadUserToDBService readUserToDBService = new ReadUserToDBService();

        File file = new File("E:\\spiderFile\\jobbole\\blog");
        try {
            for (File file1 : file.listFiles()){
                readUserToDBService.readUserFromHtml2DB(file1.getAbsolutePath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
